
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Text.RegularExpressions
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for invite_friends.
	''' </summary>
	Public Partial Class invite_friends
		Inherits BaseWebPage

		Private isGroupMode As Boolean = False
		Protected grpID As Integer = -1
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' this.pageTemplate.PageTitle = PageTitles.INVITE_FRIENDS_PAGE;
			' Put user code to initialize the page here
			Dim usr As User = LoggedUser
			Dim regParam As String = Request.Params(Constants.REGISTRATION_PARAM)

			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.INVITE_FRIENDS, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			' check if group param is given
			If Request.QueryString(Constants.GROUP_PARAM) <> Nothing AndAlso Request.QueryString(Constants.GROUP_PARAM) <> "" Then
				isGroupMode = True
			End If

			If isGroupMode Then
				' init group ID
				Try
					grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
					Dim grp As BO.Group = Groups.GetGroupByID(grpID)

					If grp = Nothing Then
						Throw New Exception(Constants.ERROR_MESSAGE_GROUP_DOESNT_EXIST_ERROR)
					End If

					txtSubject.Text = Constants.INVITE_TO_GROUP_MAIL_SUBJECT.Replace(Constants.TXT_GROUP_NAME_TAG, grp.Name)
					txtSubject.Enabled = False

					txtFrom.Text = usr.Name + " " + "<" + usr.Email + ">"
					txtFrom.Enabled = False

					txtBody.Text = GroupNotifications.ComposeGroupInviteMailBody("", grp, Nothing)
					txtBody.Enabled = False
					trUserUpperText.Visible = False
					trGroupUpperText.Visible = True

					hlBackToGroup.NavigateUrl = "../" + Constants.GROUP_WELCOME + "?" + Constants.GROUP_PARAM + "=" + grpID
					hlBackToGroup.Text = (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grp.Name)
				Catch
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End Try
			Else
				txtSubject.Text = Constants.INVITE_MAIL_SUBJECT.Replace(Constants.TXT_USER_NAME_TAG, usr.Name + " " + usr.LastName)
				txtSubject.Enabled = False

				txtFrom.Text = usr.Name + " " + usr.LastName + "<" + usr.Email + ">"
				txtFrom.Enabled = False

				txtBody.Text = Users.ComposeInviteMailBody(Constants.APPLICATION_URL + "/" + Constants.JOIN, "", usr).Trim()

				txtBody.Enabled = False
				trUserUpperText.Visible = True
				trGroupUpperText.Visible = False
			End If

			If regParam <> Nothing AndAlso regParam <> "" AndAlso Convert.ToInt32(regParam) = Constants.REGISTRATION_VALUE Then
				btnSkip.Visible = True
						' this.pageTemplate.PageTitle = PageTitles.CREATE_GROUP_PAGE;
				If isGroupMode Then
						' this.pageTemplate.PageTitle = PageTitles.JOIN_PAGE;
				Else
				End If
			End If
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()

		End Sub
#End Region

		Protected Sub btnInvite_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			lblResultMessage.Text = ""
			Dim regParam As String = Request.Params(Constants.REGISTRATION_PARAM)
			Try
				Dim invitedPeopleEmails As String() = txtTo.Text.Trim().Split(","C)
				Dim invitedUsersCount As Integer = 0
				Dim i As Integer = 0
				While i < invitedPeopleEmails.Length
					If IsEmail(invitedPeopleEmails(i).Trim()) = False Then
						lblResultMessage.Text += (Me.GetPageConstText(htPageConstants, "CONST_MsgEmailNotValid_TEXT")).Replace("[PARAM]", invitedPeopleEmails(i).Trim())
					Else
						Dim usrToInvite As User = Users.GetUserByEmail(invitedPeopleEmails(i).Trim())

						If usrToInvite = Nothing Then
							If isGroupMode Then
								lblResultMessage.Text += (Me.GetPageConstText(htPageConstants, "CONST_MsgUserNotReg_TEXT")).Replace("[PARAM]", invitedPeopleEmails(i).Trim())
							Else
								Users.SendInvitation(Constants.APPLICATION_URL + "/" + Constants.JOIN, invitedPeopleEmails(i).Trim(), txtOptionalBody.Text, LoggedUser)
								System.Math.Max(System.Threading.Interlocked.Increment(invitedUsersCount),invitedUsersCount - 1)
								lblResultMessage.Text += (Me.GetPageConstText(htPageConstants, "CONST_MsgInvSent_TEXT")).Replace("[PARAM]", invitedPeopleEmails(i).Trim())
							End If
						Else
							If isGroupMode Then
								If usrToInvite.Status = Users.STATUS_LOCKED Then
									lblResultMessage.Text += (Me.GetPageConstText(htPageConstants, "CONST_MsgUserCancelled_TEXT")).Replace("[PARAM]", invitedPeopleEmails(i).Trim())
								Else
									Dim grp As BO.Group = Groups.GetGroupByID(grpID)
									GroupNotifications.SendGroupInvitation(usrToInvite, txtOptionalBody.Text, grp)
									lblResultMessage.Text += (Me.GetPageConstText(htPageConstants, "CONST_MsgGrpInvSent_TEXT")).Replace("[PARAM]", invitedPeopleEmails(i).Trim())
								End If
ElseIf usrToInvite <> Nothing AndAlso usrToInvite.Status = Users.STATUS_LOCKED Then
								Users.SendInvitation(Constants.APPLICATION_URL + "/" + Constants.JOIN, invitedPeopleEmails(i).Trim(), txtOptionalBody.Text, LoggedUser)
								System.Math.Max(System.Threading.Interlocked.Increment(invitedUsersCount),invitedUsersCount - 1)
								lblResultMessage.Text += (Me.GetPageConstText(htPageConstants, "CONST_MsgInvSent_TEXT")).Replace("[PARAM]", invitedPeopleEmails(i).Trim())
							Else
								lblResultMessage.Text += (Me.GetPageConstText(htPageConstants, "CONST_MsgAlreadyMember_TEXT")).Replace("[PARAM1]", invitedPeopleEmails(i).Trim())
								lblResultMessage.Text = lblResultMessage.Text.Replace("[PARAM2]", "<a href='" + Constants.MEMBERS + "?id=" + usrToInvite.ID + "'>click here</a>")
							End If
						End If
					End If
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While

				btnSkip.Visible = False
				If regParam <> Nothing AndAlso regParam <> "" AndAlso Convert.ToInt32(regParam) = Constants.REGISTRATION_VALUE Then
					btnInvite.Visible = False
					btnNext.Visible = True
				End If
				lblResultMessage.Visible = True
			Catch
				lblResultMessage.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgError_TEXT")
				lblResultMessage.Visible = True
			End Try
		End Sub

		Protected Sub btnSkip_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			'Response.Redirect(Constants.MY_PROFILE + "?" + Constants.REGISTRATION_PARAM + "=" + Constants.REGISTRATION_VALUE);
			If isGroupMode Then
				Response.Redirect(Constants.UPLOAD_GROUP_PHOTO + "?" + Constants.REGISTRATION_PARAM + "=" + Constants.REGISTRATION_VALUE + "&" + Constants.GROUP_PARAM + "=" + grpID)
			Else
				Response.Redirect(Constants.UPLOAD_PHOTO + "?" + Constants.REGISTRATION_PARAM + "=" + Constants.REGISTRATION_VALUE)
			End If
		End Sub

		Private Function IsEmail(ByVal sEmail As String) As Boolean
			Dim re As New Regex("\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")
			Dim myMatch As Match = re.Match(sEmail)
			If myMatch.Success Then
				Return True
			Else
				Return False
			End If
		End Function

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






